<?php
/*
 * Copyright 2008 by Oliver Radwan, Maxwell Palmer, Nolan McNair,
 * Taylor Talmage, and Allen Tucker.  This program is part of RMH Homebase.
 * RMH Homebase is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
*/

/**
 * Functions to create, update, and retrieve information from the
 * dbDates table in the database.  This table is used with the RMHDate
 * class.  Dates are generated using the master schedule (through the
 * addWeek.php form), and retrieved by the calendar forms.
 * @version May 1, 2008
 * @author Maxwell Palmer
 */

include_once('RMHdate.php');
include_once('dbShifts.php');
include_once('dbinfo.php');

/**
 * Drops dbDates table and creates a new one.
 * Elements of dbDates:
 *  id: mm-dd-yy
 *  year
 *  month
 *  day
 *  shifts - comma delimited list of shift ids
 *  chef notes
 *  manager notes
 */
function setup_dbDates() {
   connect();
   mysql_query("DROP TABLE IF EXISTS dbDates");
   $result=mysql_query("CREATE TABLE dbDates (id CHAR(8) NOT NULL, month TEXT, day TEXT, year TEXT, shifts TEXT,
								chef_notes TEXT, mgr_notes TEXT, PRIMARY KEY (id))");
	if(!$result)
		echo mysql_error();
	mysql_close();
}

/**
 * Adds a RMHDate to the table
 * If the date already exists, the date is deleted and replaced.
 */
function insert_dbDates($d) {
   if (! $d instanceof RMHdate) {
		die ("Invalid argument for dbDates->insert_dbdates function call");
	}
   connect();
   $query = "SELECT * FROM dbDates WHERE id =\"".$d->get_id()."\"";
   $result = mysql_query ($query);
   if(mysql_num_rows($result)!=0) {
   		delete_dbDates($d);
   		connect();
   }
	$query="INSERT INTO dbDates VALUES
				(\"".$d->get_id()."\",\"".substr($d->get_id(),0,2)."\",\"".
				substr($d->get_id(),3,2)."\",\"".substr($d->get_id(),6,2).
				"\",\"".get_shifts_text($d)."\",\"".$d->get_chef_notes()."\",\"".$d->get_mgr_notes()."\")";
	$result=mysql_query($query);
    if (!$result) {
		echo ("unable to insert into dbDates: ".$d->get_id(). mysql_error());
		mysql_close();
		return false;
    }
	mysql_close();
	$shifts=$d->get_shifts();
	foreach ($shifts as $key => $value) {
	    insert_dbShifts($d->get_shift($key));
	}
	return true;
 }

/**
 * deletes a date from the table
 */
function delete_dbDates($d) {
	if (! $d instanceof RMHdate)
		die ("Invalid argument for dbShifts->remove_date function call");
	connect();
    $query="DELETE FROM dbDates WHERE id=\"".$d->get_id()."\"";
	$result=mysql_query($query);
	if (!$result) {
		echo ("unable to delete from dbDates: ".$d->get_id(). mysql_error());
		mysql_close();
		return false;
    }
    mysql_close();
    $shifts=$d->get_shifts();
	foreach ($shifts as $key => $value) {
		$s = $d->get_shift($key);
	    delete_dbShifts($s);
	}
	return true;
 }

/**
 * updates a date in the dbDates table
 */
function update_dbDates($d) {
	if (! $d instanceof RMHdate)
		die ("Invalid argument for dbDates->update_dbDates function call");
    delete_dbDates($d);
    insert_dbDates($d);
    return true;
}

/**
 * selects a date from the table
 * @return RMHDate
 */
function select_dbDates($id) {
	if(strlen($id)!=8)
		die ("Invalid argument for dbDates->select_dbDates call =". $id);
	connect();
    $query = "SELECT * FROM dbDates WHERE id =\"".$id."\"";
    $result = mysql_query ($query);
	mysql_close();
	if (!$result) {
	    echo 'Could not run query: ' . mysql_error();
	    return null;
	}
	else {
		$result_row=mysql_fetch_row($result);
		if($result_row) {
			$shifts=$result_row[4];
			//echo $shifts."\n";
			$shifts=explode("*",$shifts);
			$s=array();
			foreach($shifts as $i){
				//echo $i."\n";
				$temp=select_dbShifts($i);
				if ($temp instanceof Shift)
				   $s[$temp->get_name()]=$temp;
			}
			//$scl=select_dbSubCallLists($result_row[3]);
			$d=new RMHdate($result_row[0], $s, $result_row[5], $result_row[6]);
			return $d;
		}
		else return null;
	}
}

/**
 * @return a * delimited list of the ids of the shifts for the specified day
 */
function get_shifts_text($d) {
	$shifts=$d->get_shifts();
	$shift_text="";
	foreach ($shifts as $key => $value)
	   $shift_text=$shift_text."*".$d->get_shift($key)->get_id();
		$shift_text=substr($shift_text,1);
	return $shift_text;
}

?>